﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Camping_Project.Forms
{
    public partial class FormDispo : Form
    {
        //private int identifiant = 0;
        private int id;
        private bool type;
        private FormEncoder frmI;
        private DateTime dateSelectionnee;
        private DateTime dateLimite;

        public FormDispo(FormEncoder frm,bool type, int id, DateTime dateSelectionnee, DateTime dateLimite)
        {
            InitializeComponent();
            this.type = type;
            this.frmI = frm;
            this.dateSelectionnee = dateSelectionnee;
            this.dateLimite = dateLimite;
        }

        public FormDispo(bool type, DateTime dateSelectionnee, DateTime dateLimite)
        {
            InitializeComponent();
            this.type = type;
            this.dateSelectionnee = dateSelectionnee;
            this.dateLimite = dateLimite;
        }

        private void FormDispo_Load(object sender, EventArgs e)
        {
            labelDate.Text = "Nous sommes le : " + DateTime.Now.Date.ToString();

            for (int i = 1; i < 23; i++)
            {
                this.Controls["ec" + i].Visible = false;
            }

            dataGridView1.Rows.Clear();

            Camping_BDD campingBDD = new Camping_BDD();

            var query =
                        from emplacement in campingBDD.Emplacement
                        where emplacement.TypeEmp == type// && emplacement.Occupation==false
                        select new
                        {
                            emplacement.IDEmplacement,
                            emplacement.Occupation,
                            emplacement.TypeEmp
                        };

            var query2 =
                        from reservation in campingBDD.Reservation
                        select new
                        {
                            reservation.IDReservation,
                            reservation.IDEmplacement,
                            reservation.DateDebut,
                            reservation.DateFinApprox,
                            reservation.DateFinDef,
                            reservation.PrixTotal
                        };
            
            foreach (var i in query)
            {
                TimeSpan diff = new TimeSpan();
                DateTime test = new DateTime(2200, 12, 31);
                int idEmplacement = -1;
                int estReserve = 0;
                foreach (var j in query2)
                {
                    if (i.IDEmplacement == j.IDEmplacement)
                    {
                        //MessageBox.Show(""+i.IDEmplacement);
                        //MessageBox.Show("" + test + "  " + j.DateDebut);
                        if (DateTime.Compare(test, j.DateDebut) > 0)
                        {
                            if (DateTime.Compare(j.DateDebut, dateSelectionnee) > 0)
                            {

                                estReserve++;
                                test = j.DateDebut;
                                diff = test - dateSelectionnee;
                                //MessageBox.Show("" + diff.Days);
                                idEmplacement = j.IDEmplacement;
                                //MessageBox.Show("" + test);
                                //MessageBox.Show("" + idEmplacement);
                            }
                            if (DateTime.Compare(j.DateDebut,dateSelectionnee)<0 && DateTime.Compare((DateTime)j.DateFinDef, dateSelectionnee)>0)
                            {
                                //MessageBox.Show("est réservé");
                                idEmplacement = -1;
                                //MessageBox.Show("-1");
                                estReserve = 1;
                                //MessageBox.Show("1");
                            }
                        }
                    }
                }
                if (idEmplacement != -1 && !(diff.Days<0))
                {
                    //MessageBox.Show("Passage 1");
                    if(diff.Days>30){
                        dataGridView1.Rows.Add(idEmplacement,"n'est pas réservé dans le mois");
                        this.Controls["ec" + i.IDEmplacement].Visible = true;
                    }else{
                        //MessageBox.Show("Passage 2");
                        if (diff.Days == 0)
                        {
                            dataGridView1.Rows.Add(idEmplacement,"Réservé demain");
                            this.Controls["ec" + i.IDEmplacement].Visible = true;
                        }
                        else
                        {
                            dataGridView1.Rows.Add(idEmplacement, diff.Days);
                            this.Controls["ec" + i.IDEmplacement].Visible = true;
                        }
                    }
                }
                if (estReserve == 0)
                {
                    //MessageBox.Show("Passage 3");
                    dataGridView1.Rows.Add(i.IDEmplacement, "n'est pas réservé");
                    this.Controls["ec" + i.IDEmplacement].Visible = true;
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int ligne = dataGridView1.SelectedCells[0].RowIndex;
            this.id = (int)dataGridView1["ColumnID", ligne].Value;
            this.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        public int getIdentfiant()
        {
            return this.id;
        }

        private void Map_Click(object sender, EventArgs e)
        {

        }

        
    }
}
